package com.qing.consumer.controller;

import com.qing.api.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class UserController {


    //消费者，不应爱有service层
    //RestTemplate  供我们直接调用就可以了！注册到Spring中
    // 提供多种便捷访问远程http服务的方法，简单的Restful服务模板
    @Autowired
    private RestTemplate template;


    //Ribbon实现url应该是个变量，通过服务名来访问
//    private static  final String REST_URL_PREFIX="http://localhost:8081";
    //服务提供者 spring.application.name:
    private static final  String REST_URL_PREFIX="http://springcloud-provider-user";
    @PostMapping("/consumer/user")
    public void addUse(@RequestBody User user){
        template.postForEntity(REST_URL_PREFIX+"/user",user,Void.class);
    }

    @GetMapping("/consumer/user/{id}")
    public User getUser(@PathVariable("id") Long id){
       return template.getForObject(REST_URL_PREFIX+"/user/"+id,User.class);
    }

    @GetMapping("/consumer/user")
    public List<User> findByName(@RequestParam(value = "name",defaultValue = "",required = false) String name){
        return  template.getForObject(REST_URL_PREFIX+"/user/all?name="+name,List.class);
    }


}
